"use strict";

define('api', [
  'jquery'
], function ($) {

  var api = {};

  function ajaxPost(url, json, cb) {
    $.ajax({
      url: url,
      type: "POST",
      headers: {"Content-Type": "application/json;charset=UTF-8"},
      data: JSON.stringify(json),
      success: function (res) {
        cb && cb(null, res);
      },
      error: function (xhr) {
        cb && cb(xhr.responseText);
      }
    });
  }

  function ajaxGet(url, cb) {
    $.ajax({
      url: url,
      type: "GET",
      success: function (res) {
        cb && cb(null, res);
      },
      error: function (xhr) {
        cb && cb(xhr.responseText);
      }
    });
  }

  // 所有:
  api.register = function register(name, pwd, userType, cb) {
    var url = '/api/v1/auth/register';
    var json = {name: name, pwd: pwd, userType: userType};
    ajaxPost(url, json, cb);
  };

  // 所有:
  api.login = function login(name, pwd, userType, cb) {
    var url = '/api/v1/auth/login';
    var json = {name: name, pwd: pwd, userType: userType};
    ajaxPost(url, json, cb);
  };

  // manager:
  api.addLoc = function addLoc(cityName, zoneName, cb) {
    var url = '/api/v1/location';
    var json = {cityName: cityName, zoneName: zoneName};
    ajaxPost(url, json, cb);
  };

  // 所有:
  api.allLoc = function allLoc(cb) {
    var url = '/api/v1/location';
    ajaxGet(url, cb);
  };

  // manager:
  api.addCate = function addCate(name, cb) {
    var url = '/api/v1/category';
    var json = {name: name};
    ajaxPost(url, json, cb);
  };

  // 所有:
  api.allCate = function allCate(cb) {
    var url = '/api/v1/category';
    ajaxGet(url, cb);
  };

  // 商家:
  api.addWare = function addWare(name, categoryId, quantity, price, cb) {
    var url = '/api/v1/vendor/ware';
    var json = {name: name, categoryId: categoryId, quantity: quantity, price: price};
    ajaxPost(url, json, cb);
  };

  // 商家:
  api.allWare = function allWare(cb) {
    var url = '/api/v1/vendor/ware';
    ajaxGet(url, cb);
  };

  // 商家:
  api.addShop = function addShop(name, locationId, cb) {
    var url = '/api/v1/vendor/shop';
    var json = {name: name, locationId: locationId};
    ajaxPost(url, json, cb);
  };

  // 商家:
  api.allShop = function allShop(cb) {
    var url = '/api/v1/vendor/shop';
    ajaxGet(url, cb);
  };

  // 商家:一个商品 上架到 多个商铺,如果 shopIds=null/undefined,表示上架到该商家的全部商铺
  api.wareOnShelf = function (wareId, shopIds, cb) {
    var url = '/api/v1/vendor/ware/onshelf';
    var json = {wareId: wareId, shopIds: shopIds};
    ajaxPost(url, json, cb);
  };

  // customer
  api.customerQuery = function (locationId, categoryId, cb) {
    var url = '/api/v1/customer/ware?locationid=%locationid&categoryid=%categoryid';
    url = url.replace('%locationid', locationId).replace('%categoryid', categoryId);
    ajaxGet(url, cb);
  };


  api.addToCart = function (wareId, wareCount, cb) {
    var url = '/api/v1/customer/cart';
    var json = {wareId: wareId, wareCount: wareCount};
    ajaxPost(url, json, cb);
  };

  api.payOrder = function (orderId, cb) {
    var url = '/api/v1/customer/order/pay';
    var json = {orderId: orderId};
    ajaxPost(url, json, cb);
  };


  return api;
});
